package com.jtech.together.mvp.presenter;

import android.content.Context;
import android.os.Bundle;

import com.jtech.together.mvp.contract.ChatContract;
import com.jtech.together.util.IM;
import com.jtech.together.util.luban.Luban;
import com.jtech.together.view.activity.ChatActivity;

import java.io.File;
import java.util.List;

import cn.bmob.newim.bean.BmobIMConversation;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 聊天页面
 * Created by wuxubaiyang on 2017/5/14.
 */
public class ChatPresenter implements ChatContract.Presenter {
    private Context context;
    private ChatContract.View view;
    private String targetId;

    public ChatPresenter(Context context, ChatContract.View view, Bundle bundle) {
        this.context = context;
        this.view = view;
        //获取目标id
        if (null != bundle) {
            this.targetId = bundle.getString(ChatActivity.KEY_TARGET_ID, "");
        }
    }

    @Override
    public BmobIMConversation getConversation() {
        return IM.toConversation(targetId);
    }

    @Override
    public String getTargetId() {
        return targetId;
    }

    @Override
    public void handleImageList(List<String> imageList) {
        for (int i = 0; i < imageList.size(); i++) {
            //使用luban进行图片压缩
            Luban.get(context)
                    .load(new File(imageList.get(i)))
                    .putGear(Luban.THIRD_GEAR)
                    .asObservable()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<File>() {
                        @Override
                        public void accept(File file) throws Exception {
                            view.handleSuccess(file.getAbsolutePath());
                        }
                    });
        }
    }
}